package dictionaryTokenScanner;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import main.tokens;

public class TokenScanner 
{
	private ArrayList<String> arlDictionary;
	
	public TokenScanner(String pDicLoc)
	{
		// get file list where the path has   
		File file = new File(pDicLoc);   
		// get the folder list   
		File[] dicArray = file.listFiles(); 

		arlDictionary = new ArrayList<String>();
		int intDicCount = 0;
		
		for (int i = 0; i < dicArray.length; i++)
		{
			if (dicArray[i].getName().contains(".txt"))
			{
				intDicCount ++; 
				System.out.println("Initialize dictionary " + (intDicCount) + "(" + dicArray[i].getName() + ")");
				getCntDic(dicArray[i]);
				System.out.println("Initialize dictionary " + (intDicCount) + "(" + arlDictionary.size() + ")" + ", Finished");
			}
		}
		
		Collections.sort(arlDictionary);
	}
	
	private void getCntDic(File pCntDic)
	{
		try
		{
			FileReader reader = new FileReader(pCntDic);
			BufferedReader br = new BufferedReader(reader);
			String strCntLine = null;
			
			while((strCntLine = br.readLine()) != null) 
			{
				String s = "\\w+";
		        Pattern  pattern=Pattern.compile(s);  
		        Matcher  ma=pattern.matcher(strCntLine);  
		        String strTmp;
		        while(ma.find())
		        {
		        	strTmp = ma.group();
		        		arlDictionary.add(strTmp); 
		        }  
			}
			
			br.close();
			reader.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public ArrayList<tokens> tokenScanner(ArrayList<tokens> pTokensArray)
	{
		ArrayList<tokens> arlTokensArray = pTokensArray;
		
		for (int i = 0; i < arlTokensArray.size(); i++)
		{
			int intTempRtn = singleTokenScanner(arlTokensArray.get(i).getToken().toLowerCase());
			//***********************************************************************
			//version for keep all tokens
			//arlTokensArray.get(i).setState(intTempRtn);	
			
			//version for delete token which state is not 0
			if (intTempRtn != 0)
			{
				arlTokensArray.remove(i);
				i--;
			}
			//***********************************************************************
			
		}

		return arlTokensArray;
	}
	
	/*
	 * Function: check current token is in the dictionary or not
	 * Input: pToken -> current token
	 * Output: intState: -1(non sense); 0(not in); 1(in dictionary)  
	 */
	private int singleTokenScanner(String pToken)
	{
		int intState = 0;
		
		String s = "[a-z]+";
        Pattern  pattern=Pattern.compile(s);  
        Matcher  ma=pattern.matcher(pToken);  
        if (ma.find())
        {
        	if (arlDictionary.contains(ma.group()))
    		{
    			intState = 1;
    		}
        }
        else
        {
        	intState = -1;
        }
		
		return intState;
	}
}
